package com.hrp.analyzer;


import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hrp.analyzer.request.phantomjs.PhantomJsClient;
import com.hrp.analyzer.util.FileUtils;
import com.hrp.analyzer.util.SystemUtils;
import com.hrp.analyzer.util.compat.PrintLog;
import com.hrp.analyzer.util.compat.PrintLogDelegate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

/**
 * 解析器全局配置类 单例
 *
 * @author hrp
 * @date 2020/12/20 3:04 下午
 */
public class AnalyzerManager {
    /**
     * 单例实例
     */
    private static AnalyzerManager ANALYZER_MANAGER;
    /**
     * 是否启用自动代理,启用自动代理前需要打开这个开关
     */

    private boolean autoProxy = false;


    public boolean isAutoProxy() {
        return autoProxy;
    }


    public AnalyzerManager setAutoProxy(boolean autoProxy) {
        this.autoProxy = autoProxy;
        return this;
    }

    public static PrintLogDelegate getDelegate() {
        return PrintLog.Companion.getDelegate();
    }

    public static void setDelegate(PrintLogDelegate delegate) {
        PrintLog.Companion.setDelegate(delegate);
    }

    private AnalyzerManager() {
    }

    /**
     * 获取实例
     *
     * @return ANALYZER_MANAGER
     */
    public static AnalyzerManager me() {
        return ANALYZER_MANAGER;
    }


    public static AnalyzerManager init(@NonNull Context context) {
        return init(context, null);
    }

    public static AnalyzerManager init(@NonNull Context context, @Nullable PrintLogDelegate delegate) {
        if (ANALYZER_MANAGER != null) {
            return ANALYZER_MANAGER;
        }
        ANALYZER_MANAGER = new AnalyzerManager();
        ANALYZER_MANAGER.setDelegate(delegate);
        File filesDir = context.getExternalFilesDir(null);
        File scriptFileDir = new File(filesDir, "script");
        File spiderFile = new File(scriptFileDir, "spider.js");
        FileUtils.USER_DIR = filesDir.getPath();
        PhantomJsClient.DEFAULT_PHANTOMJS_SCRIPT = FileUtils.USER_DIR + "/script/spider.js";
        PhantomJsClient.DEFAULT_PHANTOMJS_PATH = FileUtils.USER_DIR + "/script/phantomjs" + SystemUtils.getExecuteSuffix();
        if (!scriptFileDir.exists()) {
            if (!scriptFileDir.mkdirs()) {
                return ANALYZER_MANAGER;
            }
        }
        if (!spiderFile.exists()) {
            BufferedWriter fileWriter = null;
            InputStream inputStream = null;
            try {
                fileWriter = new BufferedWriter(
                        new OutputStreamWriter(
                                new FileOutputStream(spiderFile), StandardCharsets.UTF_8));

                inputStream = context.getAssets().open("script/spider.js");
                int ch;
                do {
                    ch = inputStream.read();
                    if (ch != -1) {
                        fileWriter.write(ch);
                    }
                } while (ch != -1);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileWriter != null) {
                    try {
                        fileWriter.close();
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
        return ANALYZER_MANAGER;
    }

}
